<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>events_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_events_events_test.js.html">events_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.eventsTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.eventsTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom&#39;);
<a name="line20"></a>goog.require(&#39;goog.events&#39;);
<a name="line21"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line22"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line23"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line24"></a>goog.require(&#39;goog.string&#39;);
<a name="line25"></a>goog.require(&#39;goog.style&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.events&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line30"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line31"></a>
<a name="line32"></a>var firedEventTypes;
<a name="line33"></a>var firedEventCoordinates;
<a name="line34"></a>var firedScreenCoordinates;
<a name="line35"></a>var firedShiftKeys;
<a name="line36"></a>var firedKeyCodes;
<a name="line37"></a>var root;
<a name="line38"></a>var log;
<a name="line39"></a>var input;
<a name="line40"></a>var testButton;
<a name="line41"></a>var parentEl;
<a name="line42"></a>var childEl;
<a name="line43"></a>var coordinate = new goog.math.Coordinate(123, 456);
<a name="line44"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line45"></a>var eventCount;
<a name="line46"></a>
<a name="line47"></a>function setUpPage() {
<a name="line48"></a>  root = goog.dom.getElement(&#39;root&#39;);
<a name="line49"></a>  log = goog.dom.getElement(&#39;log&#39;);
<a name="line50"></a>  input = goog.dom.getElement(&#39;input&#39;);
<a name="line51"></a>  testButton = goog.dom.getElement(&#39;testButton&#39;);
<a name="line52"></a>  parentEl = goog.dom.getElement(&#39;parentEl&#39;);
<a name="line53"></a>  childEl = goog.dom.getElement(&#39;childEl&#39;);
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>function setUp() {
<a name="line57"></a>  stubs.reset();
<a name="line58"></a>  goog.events.removeAll(root);
<a name="line59"></a>  goog.events.removeAll(log);
<a name="line60"></a>  goog.events.removeAll(input);
<a name="line61"></a>  goog.events.removeAll(testButton);
<a name="line62"></a>  goog.events.removeAll(parentEl);
<a name="line63"></a>  goog.events.removeAll(childEl);
<a name="line64"></a>
<a name="line65"></a>  root.innerHTML = &#39;&#39;;
<a name="line66"></a>  firedEventTypes = [];
<a name="line67"></a>  firedEventCoordinates = [];
<a name="line68"></a>  firedScreenCoordinates = [];
<a name="line69"></a>  firedShiftKeys = [];
<a name="line70"></a>  firedKeyCodes = [];
<a name="line71"></a>
<a name="line72"></a>  for (var key in goog.events.EventType) {
<a name="line73"></a>    goog.events.listen(root, goog.events.EventType[key], function(e) {
<a name="line74"></a>      firedEventTypes.push(e.type);
<a name="line75"></a>      var coord = new goog.math.Coordinate(e.clientX, e.clientY);
<a name="line76"></a>      firedEventCoordinates.push(coord);
<a name="line77"></a>
<a name="line78"></a>      firedScreenCoordinates.push(
<a name="line79"></a>          new goog.math.Coordinate(e.screenX, e.screenY));
<a name="line80"></a>
<a name="line81"></a>      firedShiftKeys.push(!!e.shiftKey);
<a name="line82"></a>      firedKeyCodes.push(e.keyCode);
<a name="line83"></a>    });
<a name="line84"></a>  }
<a name="line85"></a>
<a name="line86"></a>  eventCount = {
<a name="line87"></a>    parentBubble: 0,
<a name="line88"></a>    parentCapture: 0,
<a name="line89"></a>    childCapture: 0,
<a name="line90"></a>    childBubble: 0
<a name="line91"></a>  };
<a name="line92"></a>  // Event listeners for the capture/bubble test.
<a name="line93"></a>  goog.events.listen(parentEl, goog.events.EventType.CLICK,
<a name="line94"></a>      function(e) {
<a name="line95"></a>        eventCount.parentCapture++;
<a name="line96"></a>        assertEquals(parentEl, e.currentTarget);
<a name="line97"></a>        assertEquals(childEl, e.target);
<a name="line98"></a>      }, true);
<a name="line99"></a>  goog.events.listen(childEl, goog.events.EventType.CLICK,
<a name="line100"></a>      function(e) {
<a name="line101"></a>        eventCount.childCapture++;
<a name="line102"></a>        assertEquals(childEl, e.currentTarget);
<a name="line103"></a>        assertEquals(childEl, e.target);
<a name="line104"></a>      }, true);
<a name="line105"></a>  goog.events.listen(childEl, goog.events.EventType.CLICK,
<a name="line106"></a>      function(e) {
<a name="line107"></a>        eventCount.childBubble++;
<a name="line108"></a>        assertEquals(childEl, e.currentTarget);
<a name="line109"></a>        assertEquals(childEl, e.target);
<a name="line110"></a>      });
<a name="line111"></a>  goog.events.listen(parentEl, goog.events.EventType.CLICK,
<a name="line112"></a>      function(e) {
<a name="line113"></a>        eventCount.parentBubble++;
<a name="line114"></a>        assertEquals(parentEl, e.currentTarget);
<a name="line115"></a>        assertEquals(childEl, e.target);
<a name="line116"></a>      });
<a name="line117"></a>}
<a name="line118"></a>
<a name="line119"></a>function tearDownPage() {
<a name="line120"></a>  for (var key in goog.events.EventType) {
<a name="line121"></a>    var type = goog.events.EventType[key];
<a name="line122"></a>    if (type == &#39;mousemove&#39; || type == &#39;mouseout&#39; || type == &#39;mouseover&#39;) {
<a name="line123"></a>      continue;
<a name="line124"></a>    }
<a name="line125"></a>    goog.dom.appendChild(input,
<a name="line126"></a>        goog.dom.createDom(&#39;label&#39;, null,
<a name="line127"></a>            goog.dom.createDom(&#39;input&#39;,
<a name="line128"></a>                {&#39;id&#39;: type, &#39;type&#39;: &#39;checkbox&#39;}),
<a name="line129"></a>            type,
<a name="line130"></a>            goog.dom.createDom(&#39;br&#39;)));
<a name="line131"></a>    goog.events.listen(testButton, type, function(e) {
<a name="line132"></a>      if (goog.dom.getElement(e.type).checked) {
<a name="line133"></a>        e.preventDefault();
<a name="line134"></a>      }
<a name="line135"></a>
<a name="line136"></a>      log.innerHTML += goog.string.subs(&#39;&lt;br /&gt;%s (%s, %s)&#39;,
<a name="line137"></a>          e.type, e.clientX, e.clientY);
<a name="line138"></a>    });
<a name="line139"></a>  }
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>function testMouseOver() {
<a name="line143"></a>  goog.testing.events.fireMouseOverEvent(root, null);
<a name="line144"></a>  goog.testing.events.fireMouseOverEvent(root, null, coordinate);
<a name="line145"></a>  assertEventTypes([&#39;mouseover&#39;, &#39;mouseover&#39;]);
<a name="line146"></a>  assertCoordinates([goog.style.getClientPosition(root), coordinate]);
<a name="line147"></a>}
<a name="line148"></a>
<a name="line149"></a>function testMouseOut() {
<a name="line150"></a>  goog.testing.events.fireMouseOutEvent(root, null);
<a name="line151"></a>  goog.testing.events.fireMouseOutEvent(root, null, coordinate);
<a name="line152"></a>  assertEventTypes([&#39;mouseout&#39;, &#39;mouseout&#39;]);
<a name="line153"></a>  assertCoordinates([goog.style.getClientPosition(root), coordinate]);
<a name="line154"></a>}
<a name="line155"></a>
<a name="line156"></a>function testFocus() {
<a name="line157"></a>  goog.testing.events.fireFocusEvent(root);
<a name="line158"></a>  assertEventTypes([&#39;focus&#39;]);
<a name="line159"></a>}
<a name="line160"></a>
<a name="line161"></a>function testBlur() {
<a name="line162"></a>  goog.testing.events.fireBlurEvent(root);
<a name="line163"></a>  assertEventTypes([&#39;blur&#39;]);
<a name="line164"></a>}
<a name="line165"></a>
<a name="line166"></a>function testClickSequence() {
<a name="line167"></a>  assertTrue(goog.testing.events.fireClickSequence(root));
<a name="line168"></a>  assertEventTypes([&#39;mousedown&#39;, &#39;mouseup&#39;, &#39;click&#39;]);
<a name="line169"></a>  var rootPosition = goog.style.getClientPosition(root);
<a name="line170"></a>  assertCoordinates([rootPosition, rootPosition, rootPosition]);
<a name="line171"></a>}
<a name="line172"></a>
<a name="line173"></a>function testClickSequenceWithCoordinate() {
<a name="line174"></a>  assertTrue(goog.testing.events.fireClickSequence(root, null, coordinate));
<a name="line175"></a>  assertCoordinates([coordinate, coordinate, coordinate]);
<a name="line176"></a>  assertArrayEquals([false, false, false], firedShiftKeys);
<a name="line177"></a>}
<a name="line178"></a>
<a name="line179"></a>function testTouchStart() {
<a name="line180"></a>  goog.testing.events.fireTouchStartEvent(root);
<a name="line181"></a>  goog.testing.events.fireTouchStartEvent(root, coordinate);
<a name="line182"></a>  assertEventTypes([&#39;touchstart&#39;, &#39;touchstart&#39;]);
<a name="line183"></a>  assertCoordinates([goog.style.getClientPosition(root), coordinate]);
<a name="line184"></a>}
<a name="line185"></a>
<a name="line186"></a>function testTouchMove() {
<a name="line187"></a>  goog.testing.events.fireTouchMoveEvent(root);
<a name="line188"></a>  goog.testing.events.fireTouchMoveEvent(root, coordinate, {touches: []});
<a name="line189"></a>  assertEventTypes([&#39;touchmove&#39;, &#39;touchmove&#39;]);
<a name="line190"></a>  assertCoordinates([goog.style.getClientPosition(root), coordinate]);
<a name="line191"></a>}
<a name="line192"></a>
<a name="line193"></a>function testTouchEnd() {
<a name="line194"></a>  goog.testing.events.fireTouchEndEvent(root);
<a name="line195"></a>  goog.testing.events.fireTouchEndEvent(root, coordinate);
<a name="line196"></a>  assertEventTypes([&#39;touchend&#39;, &#39;touchend&#39;]);
<a name="line197"></a>  assertCoordinates([goog.style.getClientPosition(root), coordinate]);
<a name="line198"></a>}
<a name="line199"></a>
<a name="line200"></a>function testTouchSequence() {
<a name="line201"></a>  assertTrue(goog.testing.events.fireTouchSequence(root));
<a name="line202"></a>  assertEventTypes([&#39;touchstart&#39;, &#39;touchend&#39;]);
<a name="line203"></a>  var rootPosition = goog.style.getClientPosition(root);
<a name="line204"></a>  assertCoordinates([rootPosition, rootPosition]);
<a name="line205"></a>}
<a name="line206"></a>
<a name="line207"></a>function testTouchSequenceWithCoordinate() {
<a name="line208"></a>  assertTrue(goog.testing.events.fireTouchSequence(root, coordinate));
<a name="line209"></a>  assertCoordinates([coordinate, coordinate]);
<a name="line210"></a>}
<a name="line211"></a>
<a name="line212"></a>function testClickSequenceWithEventProperty() {
<a name="line213"></a>  assertTrue(goog.testing.events.fireClickSequence(
<a name="line214"></a>      root, null, undefined, { shiftKey: true }));
<a name="line215"></a>  assertArrayEquals([true, true, true], firedShiftKeys);
<a name="line216"></a>}
<a name="line217"></a>
<a name="line218"></a>function testClickSequenceCancellingMousedown() {
<a name="line219"></a>  preventDefaultEventType(&#39;mousedown&#39;);
<a name="line220"></a>  assertFalse(goog.testing.events.fireClickSequence(root));
<a name="line221"></a>  assertEventTypes([&#39;mousedown&#39;, &#39;mouseup&#39;, &#39;click&#39;]);
<a name="line222"></a>}
<a name="line223"></a>
<a name="line224"></a>function testClickSequenceCancellingMousedownWithCoordinate() {
<a name="line225"></a>  preventDefaultEventType(&#39;mousedown&#39;);
<a name="line226"></a>  assertFalse(goog.testing.events.fireClickSequence(root, null, coordinate));
<a name="line227"></a>  assertCoordinates([coordinate, coordinate, coordinate]);
<a name="line228"></a>}
<a name="line229"></a>
<a name="line230"></a>function testClickSequenceCancellingMouseup() {
<a name="line231"></a>  preventDefaultEventType(&#39;mouseup&#39;);
<a name="line232"></a>  assertFalse(goog.testing.events.fireClickSequence(root));
<a name="line233"></a>  assertEventTypes([&#39;mousedown&#39;, &#39;mouseup&#39;, &#39;click&#39;]);
<a name="line234"></a>}
<a name="line235"></a>
<a name="line236"></a>function testClickSequenceCancellingMouseupWithCoordinate() {
<a name="line237"></a>  preventDefaultEventType(&#39;mouseup&#39;);
<a name="line238"></a>  assertFalse(goog.testing.events.fireClickSequence(root, null, coordinate));
<a name="line239"></a>  assertCoordinates([coordinate, coordinate, coordinate]);
<a name="line240"></a>}
<a name="line241"></a>
<a name="line242"></a>function testClickSequenceCancellingClick() {
<a name="line243"></a>  preventDefaultEventType(&#39;click&#39;);
<a name="line244"></a>  assertFalse(goog.testing.events.fireClickSequence(root));
<a name="line245"></a>  assertEventTypes([&#39;mousedown&#39;, &#39;mouseup&#39;, &#39;click&#39;]);
<a name="line246"></a>}
<a name="line247"></a>
<a name="line248"></a>function testClickSequenceCancellingClickWithCoordinate() {
<a name="line249"></a>  preventDefaultEventType(&#39;click&#39;);
<a name="line250"></a>  assertFalse(goog.testing.events.fireClickSequence(root, null, coordinate));
<a name="line251"></a>  assertCoordinates([coordinate, coordinate, coordinate]);
<a name="line252"></a>}
<a name="line253"></a>
<a name="line254"></a>// For a double click, IE fires selectstart instead of the second mousedown,
<a name="line255"></a>// but we don&#39;t simulate selectstart. Also, IE doesn&#39;t fire the second click.
<a name="line256"></a>var DBLCLICK_SEQ = (goog.userAgent.IE ?
<a name="line257"></a>                    [&#39;mousedown&#39;,
<a name="line258"></a>                     &#39;mouseup&#39;,
<a name="line259"></a>                     &#39;click&#39;,
<a name="line260"></a>                     &#39;mouseup&#39;,
<a name="line261"></a>                     &#39;dblclick&#39;] :
<a name="line262"></a>                    [&#39;mousedown&#39;,
<a name="line263"></a>                     &#39;mouseup&#39;,
<a name="line264"></a>                     &#39;click&#39;,
<a name="line265"></a>                     &#39;mousedown&#39;,
<a name="line266"></a>                     &#39;mouseup&#39;,
<a name="line267"></a>                     &#39;click&#39;,
<a name="line268"></a>                     &#39;dblclick&#39;]);
<a name="line269"></a>
<a name="line270"></a>
<a name="line271"></a>var DBLCLICK_SEQ_COORDS = goog.array.repeat(coordinate, DBLCLICK_SEQ.length);
<a name="line272"></a>
<a name="line273"></a>function testDoubleClickSequence() {
<a name="line274"></a>  assertTrue(goog.testing.events.fireDoubleClickSequence(root));
<a name="line275"></a>  assertEventTypes(DBLCLICK_SEQ);
<a name="line276"></a>}
<a name="line277"></a>
<a name="line278"></a>function testDoubleClickSequenceWithCoordinate() {
<a name="line279"></a>  assertTrue(goog.testing.events.fireDoubleClickSequence(root, coordinate));
<a name="line280"></a>  assertCoordinates(DBLCLICK_SEQ_COORDS);
<a name="line281"></a>}
<a name="line282"></a>
<a name="line283"></a>function testDoubleClickSequenceCancellingMousedown() {
<a name="line284"></a>  preventDefaultEventType(&#39;mousedown&#39;);
<a name="line285"></a>  assertFalse(goog.testing.events.fireDoubleClickSequence(root));
<a name="line286"></a>  assertEventTypes(DBLCLICK_SEQ);
<a name="line287"></a>}
<a name="line288"></a>
<a name="line289"></a>function testDoubleClickSequenceCancellingMousedownWithCoordinate() {
<a name="line290"></a>  preventDefaultEventType(&#39;mousedown&#39;);
<a name="line291"></a>  assertFalse(goog.testing.events.fireDoubleClickSequence(root, coordinate));
<a name="line292"></a>  assertCoordinates(DBLCLICK_SEQ_COORDS);
<a name="line293"></a>}
<a name="line294"></a>
<a name="line295"></a>function testDoubleClickSequenceCancellingMouseup() {
<a name="line296"></a>  preventDefaultEventType(&#39;mouseup&#39;);
<a name="line297"></a>  assertFalse(goog.testing.events.fireDoubleClickSequence(root));
<a name="line298"></a>  assertEventTypes(DBLCLICK_SEQ);
<a name="line299"></a>}
<a name="line300"></a>
<a name="line301"></a>function testDoubleClickSequenceCancellingMouseupWithCoordinate() {
<a name="line302"></a>  preventDefaultEventType(&#39;mouseup&#39;);
<a name="line303"></a>  assertFalse(goog.testing.events.fireDoubleClickSequence(root, coordinate));
<a name="line304"></a>  assertCoordinates(DBLCLICK_SEQ_COORDS);
<a name="line305"></a>}
<a name="line306"></a>
<a name="line307"></a>function testDoubleClickSequenceCancellingClick() {
<a name="line308"></a>  preventDefaultEventType(&#39;click&#39;);
<a name="line309"></a>  assertFalse(goog.testing.events.fireDoubleClickSequence(root));
<a name="line310"></a>  assertEventTypes(DBLCLICK_SEQ);
<a name="line311"></a>}
<a name="line312"></a>
<a name="line313"></a>function testDoubleClickSequenceCancellingClickWithCoordinate() {
<a name="line314"></a>  preventDefaultEventType(&#39;click&#39;);
<a name="line315"></a>  assertFalse(goog.testing.events.fireDoubleClickSequence(root, coordinate));
<a name="line316"></a>  assertCoordinates(DBLCLICK_SEQ_COORDS);
<a name="line317"></a>}
<a name="line318"></a>
<a name="line319"></a>function testDoubleClickSequenceCancellingDoubleClick() {
<a name="line320"></a>  preventDefaultEventType(&#39;dblclick&#39;);
<a name="line321"></a>  assertFalse(goog.testing.events.fireDoubleClickSequence(root));
<a name="line322"></a>  assertEventTypes(DBLCLICK_SEQ);
<a name="line323"></a>}
<a name="line324"></a>
<a name="line325"></a>function testDoubleClickSequenceCancellingDoubleClickWithCoordinate() {
<a name="line326"></a>  preventDefaultEventType(&#39;dblclick&#39;);
<a name="line327"></a>  assertFalse(goog.testing.events.fireDoubleClickSequence(root, coordinate));
<a name="line328"></a>  assertCoordinates(DBLCLICK_SEQ_COORDS);
<a name="line329"></a>}
<a name="line330"></a>
<a name="line331"></a>function testKeySequence() {
<a name="line332"></a>  assertTrue(goog.testing.events.fireKeySequence(
<a name="line333"></a>      root, goog.events.KeyCodes.ZERO));
<a name="line334"></a>  assertEventTypes([&#39;keydown&#39;, &#39;keypress&#39;, &#39;keyup&#39;]);
<a name="line335"></a>}
<a name="line336"></a>
<a name="line337"></a>function testKeySequenceCancellingKeydown() {
<a name="line338"></a>  preventDefaultEventType(&#39;keydown&#39;);
<a name="line339"></a>  assertFalse(goog.testing.events.fireKeySequence(
<a name="line340"></a>      root, goog.events.KeyCodes.ZERO));
<a name="line341"></a>  if (goog.userAgent.IE) {
<a name="line342"></a>    assertEventTypes([&#39;keydown&#39;, &#39;keyup&#39;]);
<a name="line343"></a>  } else {
<a name="line344"></a>    assertEventTypes([&#39;keydown&#39;, &#39;keypress&#39;, &#39;keyup&#39;]);
<a name="line345"></a>  }
<a name="line346"></a>}
<a name="line347"></a>
<a name="line348"></a>function testKeySequenceCancellingKeypress() {
<a name="line349"></a>  preventDefaultEventType(&#39;keypress&#39;);
<a name="line350"></a>  assertFalse(goog.testing.events.fireKeySequence(
<a name="line351"></a>      root, goog.events.KeyCodes.ZERO));
<a name="line352"></a>  assertEventTypes([&#39;keydown&#39;, &#39;keypress&#39;, &#39;keyup&#39;]);
<a name="line353"></a>}
<a name="line354"></a>
<a name="line355"></a>function testKeySequenceCancellingKeyup() {
<a name="line356"></a>  preventDefaultEventType(&#39;keyup&#39;);
<a name="line357"></a>  assertFalse(goog.testing.events.fireKeySequence(
<a name="line358"></a>      root, goog.events.KeyCodes.ZERO));
<a name="line359"></a>  assertEventTypes([&#39;keydown&#39;, &#39;keypress&#39;, &#39;keyup&#39;]);
<a name="line360"></a>}
<a name="line361"></a>
<a name="line362"></a>function testKeySequenceWithEscapeKey() {
<a name="line363"></a>  assertTrue(goog.testing.events.fireKeySequence(
<a name="line364"></a>      root, goog.events.KeyCodes.ESC));
<a name="line365"></a>  if (goog.userAgent.WEBKIT &amp;&amp; goog.userAgent.isVersionOrHigher(&#39;525&#39;)) {
<a name="line366"></a>    assertEventTypes([&#39;keydown&#39;, &#39;keyup&#39;]);
<a name="line367"></a>  } else {
<a name="line368"></a>    assertEventTypes([&#39;keydown&#39;, &#39;keypress&#39;, &#39;keyup&#39;]);
<a name="line369"></a>  }
<a name="line370"></a>}
<a name="line371"></a>
<a name="line372"></a>function testKeySequenceForMacActionKeysNegative() {
<a name="line373"></a>  stubs.set(goog.userAgent, &#39;GECKO&#39;, false);
<a name="line374"></a>  goog.testing.events.fireKeySequence(
<a name="line375"></a>      root, goog.events.KeyCodes.C, {&#39;metaKey&#39;: true});
<a name="line376"></a>  assertEventTypes([&#39;keydown&#39;, &#39;keypress&#39;, &#39;keyup&#39;]);
<a name="line377"></a>}
<a name="line378"></a>
<a name="line379"></a>function testKeySequenceForMacActionKeysPositive() {
<a name="line380"></a>  stubs.set(goog.userAgent, &#39;GECKO&#39;, true);
<a name="line381"></a>  stubs.set(goog.userAgent, &#39;MAC&#39;, true);
<a name="line382"></a>  goog.testing.events.fireKeySequence(
<a name="line383"></a>      root, goog.events.KeyCodes.C, {&#39;metaKey&#39;: true});
<a name="line384"></a>  assertEventTypes([&#39;keypress&#39;, &#39;keyup&#39;]);
<a name="line385"></a>}
<a name="line386"></a>
<a name="line387"></a>function testKeySequenceForOptionKeysOnMac() {
<a name="line388"></a>  // Mac uses an option (or alt) key to type non-ASCII characters. This test
<a name="line389"></a>  // verifies we can emulate key events sent when typing such non-ASCII
<a name="line390"></a>  // characters.
<a name="line391"></a>  stubs.set(goog.userAgent, &#39;WEBKIT&#39;, true);
<a name="line392"></a>  stubs.set(goog.userAgent, &#39;MAC&#39;, true);
<a name="line393"></a>
<a name="line394"></a>  var optionKeyCodes = [
<a name="line395"></a>    [0xc0, 0x00e6],  // option+&#39;
<a name="line396"></a>    [0xbc, 0x2264],  // option+,
<a name="line397"></a>    [0xbd, 0x2013],  // option+-
<a name="line398"></a>    [0xbe, 0x2265],  // option+.
<a name="line399"></a>    [0xbf, 0x00f7],  // option+/
<a name="line400"></a>    [0x30, 0x00ba],  // option+0
<a name="line401"></a>    [0x31, 0x00a1],  // option+1
<a name="line402"></a>    [0x32, 0x2122],  // option+2
<a name="line403"></a>    [0x33, 0x00a3],  // option+3
<a name="line404"></a>    [0x34, 0x00a2],  // option+4
<a name="line405"></a>    [0x35, 0x221e],  // option+5
<a name="line406"></a>    [0x36, 0x00a7],  // option+6
<a name="line407"></a>    [0x37, 0x00b6],  // option+7
<a name="line408"></a>    [0x38, 0x2022],  // option+8
<a name="line409"></a>    [0x39, 0x00aa],  // option+9
<a name="line410"></a>    [0xba, 0x2026],  // option+;
<a name="line411"></a>    [0xbb, 0x2260],  // option+=
<a name="line412"></a>    [0xdb, 0x201c],  // option+[
<a name="line413"></a>    [0xdc, 0x00ab],  // option+\
<a name="line414"></a>    [0xdd, 0x2018],  // option+]
<a name="line415"></a>    [0x41, 0x00e5],  // option+a
<a name="line416"></a>    [0x42, 0x222b],  // option+b
<a name="line417"></a>    [0x43, 0x00e7],  // option+c
<a name="line418"></a>    [0x44, 0x2202],  // option+d
<a name="line419"></a>    [0x45, 0x00b4],  // option+e
<a name="line420"></a>    [0x46, 0x0192],  // option+f
<a name="line421"></a>    [0x47, 0x00a9],  // option+g
<a name="line422"></a>    [0x48, 0x02d9],  // option+h
<a name="line423"></a>    [0x49, 0x02c6],  // option+i
<a name="line424"></a>    [0x4a, 0x2206],  // option+j
<a name="line425"></a>    [0x4b, 0x02da],  // option+k
<a name="line426"></a>    [0x4c, 0x00ac],  // option+l
<a name="line427"></a>    [0x4d, 0x00b5],  // option+m
<a name="line428"></a>    [0x4e, 0x02dc],  // option+n
<a name="line429"></a>    [0x4f, 0x00f8],  // option+o
<a name="line430"></a>    [0x50, 0x03c0],  // option+p
<a name="line431"></a>    [0x51, 0x0153],  // option+q
<a name="line432"></a>    [0x52, 0x00ae],  // option+r
<a name="line433"></a>    [0x53, 0x00df],  // option+s
<a name="line434"></a>    [0x54, 0x2020],  // option+t
<a name="line435"></a>    [0x56, 0x221a],  // option+v
<a name="line436"></a>    [0x57, 0x2211],  // option+w
<a name="line437"></a>    [0x58, 0x2248],  // option+x
<a name="line438"></a>    [0x59, 0x00a5],  // option+y
<a name="line439"></a>    [0x5a, 0x03a9]   // option+z
<a name="line440"></a>  ];
<a name="line441"></a>
<a name="line442"></a>  for (var i = 0; i &lt; optionKeyCodes.length; ++i) {
<a name="line443"></a>    firedEventTypes = [];
<a name="line444"></a>    firedKeyCodes = [];
<a name="line445"></a>    var keyCode = optionKeyCodes[i][0];
<a name="line446"></a>    var keyPressKeyCode = optionKeyCodes[i][1];
<a name="line447"></a>    goog.testing.events.fireNonAsciiKeySequence(
<a name="line448"></a>        root, keyCode, keyPressKeyCode, {&#39;altKey&#39;: true});
<a name="line449"></a>    assertEventTypes([&#39;keydown&#39;, &#39;keypress&#39;, &#39;keyup&#39;]);
<a name="line450"></a>    assertArrayEquals([keyCode, keyPressKeyCode, keyCode], firedKeyCodes);
<a name="line451"></a>  }
<a name="line452"></a>}
<a name="line453"></a>
<a name="line454"></a>var CONTEXTMENU_SEQ =
<a name="line455"></a>    goog.userAgent.WINDOWS ? [&#39;mousedown&#39;, &#39;mouseup&#39;, &#39;contextmenu&#39;] :
<a name="line456"></a>    goog.userAgent.GECKO ? [&#39;mousedown&#39;, &#39;contextmenu&#39;, &#39;mouseup&#39;] :
<a name="line457"></a>    goog.userAgent.WEBKIT &amp;&amp; goog.userAgent.MAC ?
<a name="line458"></a>        [&#39;mousedown&#39;, &#39;contextmenu&#39;, &#39;mouseup&#39;, &#39;click&#39;] :
<a name="line459"></a>    [&#39;mousedown&#39;, &#39;contextmenu&#39;, &#39;mouseup&#39;];
<a name="line460"></a>
<a name="line461"></a>function testContextMenuSequence() {
<a name="line462"></a>  assertTrue(goog.testing.events.fireContextMenuSequence(root));
<a name="line463"></a>  assertEventTypes(CONTEXTMENU_SEQ);
<a name="line464"></a>}
<a name="line465"></a>
<a name="line466"></a>function testContextMenuSequenceWithCoordinate() {
<a name="line467"></a>  assertTrue(goog.testing.events.fireContextMenuSequence(root, coordinate));
<a name="line468"></a>  assertEventTypes(CONTEXTMENU_SEQ);
<a name="line469"></a>  assertCoordinates(goog.array.repeat(coordinate, CONTEXTMENU_SEQ.length));
<a name="line470"></a>}
<a name="line471"></a>
<a name="line472"></a>function testContextMenuSequenceCancellingMousedown() {
<a name="line473"></a>  preventDefaultEventType(&#39;mousedown&#39;);
<a name="line474"></a>  assertFalse(goog.testing.events.fireContextMenuSequence(root));
<a name="line475"></a>  assertEventTypes(CONTEXTMENU_SEQ);
<a name="line476"></a>}
<a name="line477"></a>
<a name="line478"></a>function testContextMenuSequenceCancellingMouseup() {
<a name="line479"></a>  preventDefaultEventType(&#39;mouseup&#39;);
<a name="line480"></a>  assertFalse(goog.testing.events.fireContextMenuSequence(root));
<a name="line481"></a>  assertEventTypes(CONTEXTMENU_SEQ);
<a name="line482"></a>}
<a name="line483"></a>
<a name="line484"></a>function testContextMenuSequenceCancellingContextMenu() {
<a name="line485"></a>  preventDefaultEventType(&#39;contextmenu&#39;);
<a name="line486"></a>  assertFalse(goog.testing.events.fireContextMenuSequence(root));
<a name="line487"></a>  assertEventTypes(CONTEXTMENU_SEQ);
<a name="line488"></a>}
<a name="line489"></a>
<a name="line490"></a>function testContextMenuSequenceFakeMacWebkit() {
<a name="line491"></a>  stubs.set(goog.userAgent, &#39;WINDOWS&#39;, false);
<a name="line492"></a>  stubs.set(goog.userAgent, &#39;MAC&#39;, true);
<a name="line493"></a>  stubs.set(goog.userAgent, &#39;WEBKIT&#39;, true);
<a name="line494"></a>  assertTrue(goog.testing.events.fireContextMenuSequence(root));
<a name="line495"></a>  assertEventTypes([&#39;mousedown&#39;, &#39;contextmenu&#39;, &#39;mouseup&#39;, &#39;click&#39;]);
<a name="line496"></a>}
<a name="line497"></a>
<a name="line498"></a>function testCaptureBubble_simple() {
<a name="line499"></a>  assertTrue(goog.testing.events.fireClickEvent(childEl));
<a name="line500"></a>  assertObjectEquals({
<a name="line501"></a>    parentCapture: 1,
<a name="line502"></a>    childCapture: 1,
<a name="line503"></a>    childBubble: 1,
<a name="line504"></a>    parentBubble: 1
<a name="line505"></a>  }, eventCount);
<a name="line506"></a>}
<a name="line507"></a>
<a name="line508"></a>function testCaptureBubble_preventDefault() {
<a name="line509"></a>  goog.events.listen(childEl, goog.events.EventType.CLICK,
<a name="line510"></a>      function(e) {
<a name="line511"></a>        e.preventDefault();
<a name="line512"></a>      });
<a name="line513"></a>  assertFalse(goog.testing.events.fireClickEvent(childEl));
<a name="line514"></a>  assertObjectEquals({
<a name="line515"></a>    parentCapture: 1,
<a name="line516"></a>    childCapture: 1,
<a name="line517"></a>    childBubble: 1,
<a name="line518"></a>    parentBubble: 1
<a name="line519"></a>  }, eventCount);
<a name="line520"></a>}
<a name="line521"></a>
<a name="line522"></a>function testCaptureBubble_stopPropagationParentCapture() {
<a name="line523"></a>  goog.events.listen(parentEl, goog.events.EventType.CLICK,
<a name="line524"></a>      function(e) {
<a name="line525"></a>        e.stopPropagation();
<a name="line526"></a>      }, true /* capture */);
<a name="line527"></a>  assertTrue(goog.testing.events.fireClickEvent(childEl));
<a name="line528"></a>  assertObjectEquals({
<a name="line529"></a>    parentCapture: 1,
<a name="line530"></a>    childCapture: 0,
<a name="line531"></a>    childBubble: 0,
<a name="line532"></a>    parentBubble: 0
<a name="line533"></a>  }, eventCount);
<a name="line534"></a>}
<a name="line535"></a>
<a name="line536"></a>function testCaptureBubble_stopPropagationChildCapture() {
<a name="line537"></a>  goog.events.listen(childEl, goog.events.EventType.CLICK,
<a name="line538"></a>      function(e) {
<a name="line539"></a>        e.stopPropagation();
<a name="line540"></a>      }, true /* capture */);
<a name="line541"></a>  assertTrue(goog.testing.events.fireClickEvent(childEl));
<a name="line542"></a>  assertObjectEquals({
<a name="line543"></a>    parentCapture: 1,
<a name="line544"></a>    childCapture: 1,
<a name="line545"></a>    childBubble: 0,
<a name="line546"></a>    parentBubble: 0
<a name="line547"></a>  }, eventCount);
<a name="line548"></a>}
<a name="line549"></a>
<a name="line550"></a>function testCaptureBubble_stopPropagationChildBubble() {
<a name="line551"></a>  goog.events.listen(childEl, goog.events.EventType.CLICK,
<a name="line552"></a>      function(e) {
<a name="line553"></a>        e.stopPropagation();
<a name="line554"></a>      });
<a name="line555"></a>  assertTrue(goog.testing.events.fireClickEvent(childEl));
<a name="line556"></a>  assertObjectEquals({
<a name="line557"></a>    parentCapture: 1,
<a name="line558"></a>    childCapture: 1,
<a name="line559"></a>    childBubble: 1,
<a name="line560"></a>    parentBubble: 0
<a name="line561"></a>  }, eventCount);
<a name="line562"></a>}
<a name="line563"></a>
<a name="line564"></a>function testCaptureBubble_stopPropagationParentBubble() {
<a name="line565"></a>  goog.events.listen(parentEl, goog.events.EventType.CLICK,
<a name="line566"></a>      function(e) {
<a name="line567"></a>        e.stopPropagation();
<a name="line568"></a>      });
<a name="line569"></a>  assertTrue(goog.testing.events.fireClickEvent(childEl));
<a name="line570"></a>  assertObjectEquals({
<a name="line571"></a>    parentCapture: 1,
<a name="line572"></a>    childCapture: 1,
<a name="line573"></a>    childBubble: 1,
<a name="line574"></a>    parentBubble: 1
<a name="line575"></a>  }, eventCount);
<a name="line576"></a>}
<a name="line577"></a>
<a name="line578"></a>function testMixinListenable() {
<a name="line579"></a>  var obj = {};
<a name="line580"></a>  obj.doFoo = goog.testing.recordFunction();
<a name="line581"></a>
<a name="line582"></a>  goog.testing.events.mixinListenable(obj);
<a name="line583"></a>
<a name="line584"></a>  obj.doFoo();
<a name="line585"></a>  assertEquals(1, obj.doFoo.getCallCount());
<a name="line586"></a>
<a name="line587"></a>  var handler = goog.testing.recordFunction();
<a name="line588"></a>  goog.events.listen(obj, &#39;test&#39;, handler);
<a name="line589"></a>  obj.dispatchEvent(&#39;test&#39;);
<a name="line590"></a>  assertEquals(1, handler.getCallCount());
<a name="line591"></a>  assertEquals(obj, handler.getLastCall().getArgument(0).target);
<a name="line592"></a>
<a name="line593"></a>  goog.events.unlisten(obj, &#39;test&#39;, handler);
<a name="line594"></a>  obj.dispatchEvent(&#39;test&#39;);
<a name="line595"></a>  assertEquals(1, handler.getCallCount());
<a name="line596"></a>
<a name="line597"></a>  goog.events.listen(obj, &#39;test&#39;, handler);
<a name="line598"></a>  obj.dispose();
<a name="line599"></a>  obj.dispatchEvent(&#39;test&#39;);
<a name="line600"></a>  assertEquals(1, handler.getCallCount());
<a name="line601"></a>}
<a name="line602"></a>
<a name="line603"></a>
<a name="line604"></a>/**
<a name="line605"></a> * Assert that the list of events given was fired, in that order.
<a name="line606"></a> */
<a name="line607"></a>function assertEventTypes(list) {
<a name="line608"></a>  assertArrayEquals(list, firedEventTypes);
<a name="line609"></a>}
<a name="line610"></a>
<a name="line611"></a>
<a name="line612"></a>/**
<a name="line613"></a> * Assert that the list of event coordinates given was caught, in that order.
<a name="line614"></a> */
<a name="line615"></a>function assertCoordinates(list) {
<a name="line616"></a>  assertArrayEquals(list, firedEventCoordinates);
<a name="line617"></a>  assertArrayEquals(list, firedScreenCoordinates);
<a name="line618"></a>}
<a name="line619"></a>
<a name="line620"></a>
<a name="line621"></a>/** Prevent default the event of the given type on the root element. */
<a name="line622"></a>function preventDefaultEventType(type) {
<a name="line623"></a>  goog.events.listen(root, type, preventDefault);
<a name="line624"></a>}
<a name="line625"></a>
<a name="line626"></a>function preventDefault(e) {
<a name="line627"></a>  e.preventDefault();
<a name="line628"></a>}
</pre>


</body>
</html>
